import time
from telnetlib import EC

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.wait import WebDriverWait


class CommonDriver:
    def get_driver(self):
        #设置连接信息
        desc = {"platformName": "Android",  #操作系统的名字
                "platformVersion": "7.1.2",  #操作系统版本的号
                "appPackage": "com.dangdang.buy2",  #包名
                "appActivity": "com.dangdang.buy2.StartupActivity",  #Activity名
                'noReset': False,  # 设置为False以便在每次测试时重置应用的状态
                'unicodeKeyboard': True,  # 使用unicode键盘输入，适用于输入中文等特殊字符
                "udid": "127.0.0.1:21513"  #设备名
                }
        #Appium Server的服务地址
        command_executor_url = 'http://127.0.0.1:4723/wd/hub'
        #打开app
        driver = webdriver.Remote(command_executor=command_executor_url, desired_capabilities=desc)
        # 设置隐式等待
        driver.implicitly_wait(20)
        #返回driver
        return driver


if __name__ == '__main__':
    #打开APP
    # dr = CommonDriver().get_driver()

    # #点击同意
    # dr.find_element_by_id('com.dangdang.buy2:id/tv_sure').click()
    # #点击同意
    # dr.find_element_by_id('com.dangdang.buy2:id/tv_sure').click()
    # time.sleep(10)
    # #点击跳过
    # dr.find_element_by_id('com.dangdang.buy2:id/close').click()
    # time.sleep(10)
    # #点击取消
    # dr.find_element_by_id('com.dangdang.buy2:id/dialog_cancel_tv').click()

    # 打开APP
    driver = CommonDriver().get_driver()
    # driver.find_element(by=AppiumBy.XPATH,value='//android.widget.TextView[contains(@text,"不")]/following-sibling::*[1]').click()
    # el1 = driver.find_element(by=AppiumBy.ID, value="com.dangdang.buy2:id/tv_sure")
    # el1.click()
    # el2 = driver.find_element(by=AppiumBy.ID, value="com.dangdang.buy2:id/tv_sure")
    # el2.click()
    # el3 = driver.find_element(by=AppiumBy.ID, value="com.dangdang.buy2:id/close")
    # el3.click()
    # el4 = driver.find_element(by=AppiumBy.ID, value="com.dangdang.buy2:id/dialog_cancel_tv")
    # el4.click()

    # el1 = driver.find_element(by=AppiumBy.ID, value="com.dangdang.buy2:id/tv_sure")
    # el1.click()
    # el2 = driver.find_element(by=AppiumBy.ID, value="com.dangdang.buy2:id/tv_sure")
    # el2.click()
    # el3 = driver.find_element(by=AppiumBy.ID, value="com.dangdang.buy2:id/close")
    # el3.click()
    # el4 = driver.find_element(by=AppiumBy.ID, value="com.dangdang.buy2:id/dialog_cancel_tv")
    # el4.click()
    # el6 = driver.find_element(by=AppiumBy.XPATH, value="//android.widget.TextView[@text=\"\"]")
    # el6.click()
    # el7 = driver.find_element(by=AppiumBy.ID, value="com.dangdang.buy2:id/et_search")
    # el7.send_keys("余华")
    # el7.clear()

    # el1 = driver.find_element(by=AppiumBy.ID, value="com.dangdang.buy2:id/tv_sure")
    # el1.click()
    # el2 = driver.find_element(by=AppiumBy.ID, value="com.dangdang.buy2:id/tv_sure")
    # el2.click()
    # el3 = driver.find_element(by=AppiumBy.ID, value="com.dangdang.buy2:id/close")
    # el3.click()
    # el4 = driver.find_element(by=AppiumBy.ID, value="com.dangdang.buy2:id/dialog_cancel_tv")
    # el4.click()
    # time.sleep(5)
    # driver.swipe(160,180,330,180)
    # time.sleep(5)
    # driver.swipe(160,180,330,180)
    # time.sleep(5)
    # driver.swipe(160,180,330,180)
    # time.sleep(5)
    # driver.tap([(287,511)],300)

    # 定位一元素
    # el4 = driver.find_element(by=AppiumBy.ID, value='com.dangdang.buy2:id/iv_title')
    # # 实例化一个对像用于按住和拖动
    # a = TouchAction(driver)
    # long_press(元素)，按住元素
    # move_to(x,y)，移动到指定的坐标
    # release()，松开按住的元素
    # perform()，立即执行
    # a.long_press(el4).move_to(x=510,y=568-400).release().perform()
    # time.sleep(4)
    # a.long_press(el4).move_to(x=510,y=568+400).release().perform()
    #
    # time.sleep(5)
    # driver.swipe(300,500,300,500-200)
    # time.sleep(6)
    # driver.swipe(300,500,300,500+200)

    el1 = driver.find_element(by=AppiumBy.ID, value="com.dangdang.buy2:id/tv_sure")
    el1.click()
    print(f'当前时间为{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}')
    try:
          # WebDriverWait(driver,2000) 设置显示等待，最大等待时间为2000秒
          # until(条件)  直到满足xx条件
          # EC.visibility_of_element_located(元组)   定位元组中描述的元素
          # (AppiumBy.ID,'值') 定义定位方式和对应的值
          el2 = WebDriverWait(driver, 2000).until(EC.visibility_of_element_located((AppiumBy.ID,'com.dangdang.buy2:id/tv_sure')))
          el2.click()
    except Exception as e:
          print(e)
    finally:
          print(f'当前时间为{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}')

